#
#    This file is part of the substandard lisp implementation of Standard Lisp
#    Copyright (C) 2011 Nicky Nickell
#
#    substandard lisp is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

import os
import re
import sys
import yaml

directory = sys.argv[1]
r = re.compile(r'\*\/(.*)\/\*(.*)\*\/', re.DOTALL)

table = {}

for root, dirs, files in os.walk(directory):
    for name in files:
        path = os.path.join(root, name)
        category = root[len(directory)+1:]
        if path.endswith('.js') or path.endswith('.lsp') or path.endswith('.coffee'):
            m = r.findall(open(path).read())[0]

            y = yaml.load(m[1])
            y['code'] = m[0].strip()
            if 'tests' in y:
                y['tests'] = y['tests'].splitlines()

            if 'doc' in y:
              y['doc'] = y['doc'].rstrip()

            if path.endswith('.js'):
              y['lang'] = 'js'
            elif path.endswith('.lsp'):
              y['lang'] = 'lsp'
            elif path.endswith('.coffee'):
              y['lang'] = 'coffee'

            if category not in table:
              table[category] = {}

            table[category][y['name']] = y

print yaml.dump(table)
